package com.qf.filter;

import com.qf.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;

/**
 * author:z
 * time:2022/1/7 11:30
 * description:
 */
@Component
public class AuthFilter implements GlobalFilter, Ordered {

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1.获取请求对象
        ServerHttpRequest request = exchange.getRequest();
        //2.获取响应对象
        ServerHttpResponse response = exchange.getResponse();
        //3.获取用户访问url路径
        String path = request.getURI().getPath();
        //4.判断用户访问的url是注册或者是登录路径 无条件放行
        if(path.contains("/login")){
            //放行
            return chain.filter(exchange);
        }
        //5.到用户浏览器中cookie获取token令牌
        MultiValueMap<String, HttpCookie> cookies = request.getCookies();
        HttpCookie cookie = cookies.getFirst("token");
        //6.判断如果令牌获取不到，就抛出401权限不足异常
        if(cookie==null){
            //响应状态码401
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            //返回响应
            return response.setComplete();
        }
        //redis根据jti去取jwt
//        String jwt = (String) redisTemplate.boundValueOps("jti").get();
        String jwt = String.valueOf(redisTemplate.boundHashOps("jti").values());
        //获取jwt
//        String token = cookie.getValue();
        //判断jwt是否为空，如果为空也抛出401权限不足异常
        if(StringUtils.isEmpty(jwt)){
            //响应状态码401
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            //返回响应
            return response.setComplete();
        }
        //7.解析token令牌，如果无法解析，那么用户无权限访问，抛出401权限不足异常
        try {
//            Claims claims = JwtUtil.parseJWT(token);
//            String jwt = claims.getSubject();
            //8.获取用户的用户名，角色，权限，权限信息放到请求头中携带
            request.mutate().header("Authentication",jwt);
        } catch (Exception e) {
            e.printStackTrace();
            //响应状态码401
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            //返回响应
            return response.setComplete();
        }
        //9.放行
        return chain.filter(exchange);
    }

    //获取请求对象
    //获取响应对象
    //获取客户端的url
    //查看用户的url是否是注册或者登录  如果是的话就无条件放行
    //不是的话就去用户浏览器中获取token
    //如果token获取就抛出401权限不足异常
    //去redis中获取jti
    //如果jti获取失败为空或就返回401权限不足
    //判断用户的权限 将数据带到请求头中
    //放行

    @Override
    public int getOrder() {
        return 0;
    }
}
